<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/7/3
 * Time: 上午11:42
 */

namespace app\admin\controller;


use app\src\mall\logic\MallLogic;

class Mall extends Admin
{
    /**
     * 商城列表
     */
    public function queryMall()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $result = (new MallLogic)->mallList();

            if($result['status']){
                $this->success('获取商城列表成功', null, $result['info']);
            }else{
                $this->error('获取商城列表失败', null, $result['info']);
            }
        }
    }

    /**
     * 添加商城商家
     */
    public function addMall()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $name = $this->_param('name');
            if(empty($name)) $this->error('商城商家名称缺失');

            $logo = $this->_param('logo');
            if(empty($logo)) $this->error('商城商家logo缺失');

            $synopsis = $this->_param('synopsis');
            if(empty($synopsis)) $this->error('商城商家简介缺失');

            $link = $this->_param('link');
            if(empty($link)) $this->error('商城商家链接缺失');

            $display_order = $this->_param('display_order',0);

            $entity = [
                'name' => $name,
                'logo' => $logo,
                'synopsis' => $synopsis,
                'link' => $link,
                'display_order' => $display_order
            ];

            $result = (new MallLogic)->addMall($entity);

            if($result['status']){
                $this->success('添加商城商家成功', null, $result['info']);
            }else{
                $this->error('添加商城商家失败', null, $result['info']);
            }
        }
    }

    /**
     * 修改商城商家
     */
    public function edit()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $mall_id = $this->_param('mall_id');
            if(empty($mall_id)) $this->error('商城商家id缺失');

            $name = $this->_param('name');
            if(empty($name)) $this->error('商城商家名称缺失');

            $logo = $this->_param('logo');
            if(empty($logo)) $this->error('商城商家logo缺失');

            $synopsis = $this->_param('synopsis');
            if(empty($synopsis)) $this->error('商城商家简介缺失');

            $link = $this->_param('link');
            if(empty($link)) $this->error('商城商家链接缺失');

            $display_order = $this->_param('display_order');

            $entity = [
                'name' => $name,
                'logo' => $logo,
                'synopsis' => $synopsis,
                'link' => $link
            ];

            if($display_order != '') $entity['display_order'] = $display_order;

            $result = (new MallLogic)->editMall($mall_id, $entity);

            if($result['status']){
                $this->success('修改商城商家成功', null, $result['info']);
            }else{
                $this->error('修改商城商家失败', null, $result['info']);
            }
        }
    }

    /**
     * 商城商家详情
     */
    public function detail()
    {
        if(!$this->isBackAdmin()){
            $this->error('没有权限查看此内容');
        }

        $mall_id = $this->_param('mall_id');
        if(empty($mall_id)) $this->error('商城商家id缺失');

        $result = (new MallLogic)->mallDetail($mall_id);

        if($result['status']){
            $this->success('获取商城商家详情成功', null, $result['info']);
        }else{
            $this->error('获取商城商家详情失败', null, $result['info']);
        }
    }

    /**
     * 删除商城商家
     */
    public function delete()
    {
        if(!$this->isBackAdmin()){
            $this->error('没有权限查看此内容');
        }

        $mall_id = $this->_param('mall_id');
        if(empty($mall_id)) $this->error('商城商家id缺失');

        $result = (new MallLogic)->pretendDelete(['id' => $mall_id]);

        if($result['status']){
            $this->success('操作成功', null, $result['info']);
        }else{
            $this->error('操作失败', null, $result['info']);
        }
    }
}